<?php

require_once '../include/common.inc.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

getMyParam('action');
getMyParam('start_date');
getMyParam('end_date');
getMyParam('user');


if ($start_date && $end_date && $user != '') {
    $filename = 'order_' . $user . '_' . date('Ymd',$start_date) . '_' . date('Ymd',$end_date) . '.xls';
} elseif ($user != '') {
    $filename = 'order_' . $user . '.xls';
} elseif ($start_date && $end_date) {
    $filename = 'order_' . date('Ymd',$start_date) . '_' . date('Ymd',$end_date) . '.xls';
} else {
    $filename = 'order.xls';
}

//echo $filename;exit;

if ($action == "export") {
    //header("Content-type:application/octet-stream");
    header("Content-type:application/vnd.ms-excel");
    header("Accept-Ranges:bytes");
    //header("Accept-Length:".filesize($file_path));
    header("Content-Disposition:attachment;filename=".$file_name);


    //$filename = "../cache/$filename";
    
    $sql_where = ' WHERE 1=1';
    if (trim($user)  != "") {
        $sql = "SELECT `id` FROM `user` WHERE `username` LIKE '%$user%'";
        $query = $db->query($sql);
        while($rs = $db->fetch_array($query)) {
            $user_id_ary[] = $rs['id'];
        }

        if (!empty($user_id_ary)) {
            $user_id_str = implode(",", $user_id_ary);
            $sql_where .= " AND `user_id` IN (" . $user_id_str . ")";
        } else {
            $no_user = true;
        }
    }
    if ($start_date && $end_date) {
        $sql_where .= " AND `date`>$start_date AND `date` < $end_date";
    }
    
    $sql = "SELECT `id`, `product_info`, `totalcount`, `totalcost`, `user_id`, `date` FROM `order` " . $sql_where . " ORDER BY `date` DESC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $out_ary[] = $rs;
        $user_id_ary[] = $rs['user_id'];
    }

    $user_info_ary = array();
    if (!empty($user_id_ary)) {
        $user_id_str = implode(",", $user_id_ary);
        $sql = "SELECT `id`, `username`, `firstname`, `lastname`, `type` FROM `user` WHERE `id` IN (" . $user_id_str . ")";
        $query = $db->query($sql);
        while($rs = $db->fetch_array($query)) {
            $user_info_ary[$rs['id']] = $rs;
            $user_info_ary[$rs['id']]['money_type'] = $rs['type'] == 1 ? "HKD" : "USD";
        }
    }
    
    $data = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
<title>Untitled Document</title>
<style>
<!--
@page
	{margin:1.0in .75in 1.0in .75in;
	mso-header-margin:.5in;
	mso-footer-margin:.5in;}
br
	{mso-data-placement:same-cell;}
.style0
	{mso-number-format:General;
	text-align:general;
	vertical-align:bottom;
	white-space:nowrap;
	mso-rotate:0;
	mso-background-source:auto;
	mso-pattern:auto;
	color:windowtext;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	mso-font-charset:136;
	mso-protection:locked visible;
	mso-style-id:0;}
td
	{mso-style-parent:style0;
	padding-top:1px;
	padding-right:1px;
	padding-left:1px;
	mso-ignore:padding;
	color:windowtext;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	mso-font-charset:136;
	text-align:general;
	vertical-align:bottom;
	mso-background-source:auto;
	mso-pattern:auto;
	mso-protection:locked visible;
	white-space:nowrap;
	mso-rotate:0;}
.xl24
	{mso-style-parent:style0;
	font-size:8.0pt;
	font-family:Arial, sans-serif;
	mso-font-charset:0;}
.xl25
	{mso-style-parent:style0;
	font-size:8.0pt;
	font-weight:700;
	font-family:Arial, sans-serif;
	mso-font-charset:0;}
.xl26
	{mso-style-parent:style0;
	font-weight:700;}
ruby
	{ruby-align:left;}
rt
	{color:windowtext;
	font-size:9.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	mso-font-charset:136;
	mso-char-type:none;
	display:none;}
-->
</style>
</head>

<body>
<table width="100%" border="1" cellspacing="0" cellpadding="5">
<tr>
<td class=xl25>Item No.</td>
<td class=xl25>Date</td>
<td class=xl25>User(Customer)</td>
<td class=xl25>Total Count</td>
<td class=xl25>Total Price</td>
</tr>';
    
    //$s = "OrderId\tDate\tUser(Customer)\tTotal Count\tTotal Price\n";
    
    foreach($out_ary as $v) {
        $date = date('d/M/Y', $v['date']);
        //$s .= $v['id'] . "\t" . $date . "\t" . $user_info_ary[$v['user_id']]['username'] . "(" . $user_info_ary[$v['user_id']]['firstname'] . ")" . "\t" . $v['totalcount'] . "\t" . $v['totalcost'] . " " . $user_info_ary[$v['user_id']]['money_type'] . "\n";
        $data .= "<tr>
					  <td class=xl24>" . $v['id'] . "</td> 
					  <td class=xl24>" . $date . "</td> 
					  <td class=xl24>" . $user_info_ary[$v['user_id']]['username'] . "(" . $user_info_ary[$v['user_id']]['firstname'] . ")" . "</td> 
					  <td class=xl24>" . $v['totalcost'] . "</td> 
					  <td class=xl24>" . $user_info_ary[$v['user_id']]['money_type'] . "</td></tr>";
    }
    
    
    $data .= "
</table>
</body>
</html>";
    echo $data;
    //writeover($filename,$s);
    //header("location:" . $filename);
    //@unlink($filename);
}

?>